//
//  StatusTableViewCell.swift
//  TYHWeibo
//
//  Created by IOS on 15/8/4.
//  Copyright © 2015年 IOS. All rights reserved.
//

import UIKit

///微博 Cell 的标示符枚举
enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    static func cellId(status:Status) -> String {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatusTableViewCell: UITableViewCell {

    ///微博数据模型
    var status: Status? {
        didSet {
            topView.status = status
            contentLabel.text = status?.text ?? ""
            
            statusPictureView.status = status
            pictureWidthCons?.constant = statusPictureView.bounds.width
            pictureHeightCons?.constant = statusPictureView.bounds.height
            pictureTopCons?.constant = (statusPictureView.bounds.height) == 0 ? 0 : 8
        }
    }
    
    //图片约束
    var pictureWidthCons: NSLayoutConstraint?
    var pictureHeightCons: NSLayoutConstraint?
    var pictureTopCons: NSLayoutConstraint?
    
    //计算行高
    func rowHeight(status: Status) -> CGFloat {
        self.status = status
        layoutIfNeeded()
        // 返回底部视图的最大高度
        return CGRectGetMaxY(statusBottomView.frame)
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     func setupUI() {
        //添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(statusPictureView)
        contentView.addSubview(statusBottomView)
        //自动布局
        //顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        //内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        //宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -16))
        
//        //图片视图
//        let cons = statusPictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
//        pictureHeightCons = statusPictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureWidthCons = statusPictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureTopCons = statusPictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        //底部视图
        statusBottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: statusPictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
//        statusBottomView.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: contentView, size: nil)
        
    }
    
    ///懒加载
    private lazy var topView: TopView = TopView()
    lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 16
        return label
    }()
    lazy var statusBottomView: StatusBottomView = StatusBottomView()
    lazy var statusPictureView: StatusPictureView = StatusPictureView()
}
